package com.hebeu.group.controller;

import com.hebeu.group.pojo.Cart;
import com.hebeu.group.pojo.Orders;
import com.hebeu.group.service.CartService;
import com.hebeu.group.service.FoodService;
import com.hebeu.group.service.OrdersService;
import com.hebeu.group.service.TableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.Date;
import java.util.List;
@Controller
public class OrdersController {
    private OrdersService ordersService;
    private CartService cartService;
    private FoodService foodService;
    private TableService tableService;

    @Autowired
    public OrdersController(OrdersService ordersService) {
        this.ordersService = ordersService;
    }

    /**
     * 提交订单
     * @param userId
     */
    @RequestMapping(value = "/orders/add")
    @ResponseBody
    public void addOrder(int userId){
        Orders orders=new Orders();
        List<Cart> list=cartService.selectAllCarts(userId);
        orders.setoDate(new Date().toString());
        Double sum=0.00d;
        for (Cart cart:list){
            sum=cart.getFoodCount()*foodService.selectSingleFood(cart.getFoodId()).getfPrice();
        }
        orders.setoPrice(sum);
        orders.settId(userId);
        orders.setIsCheckout(1);
        ordersService.addOrder(orders);
        for (Cart cart:list){
            cart.setIsEffective(1);
            cartService.updateEffective(cart);
        }
    }

}
